define(function(require, exports, module) {

	require("modal");

	var $ = jQuery;
	var server = ".";	//服务器地址
	var tpl = "modules";	//模板地址（页面）
	var LOGIN_LOCATION = "login.html";
	var HAS_PERMS = "/api/perms";	//判断是否有权限
	var HAS_ROLES = "/api/roles";	//判断是否有角色
	var URL_DICT = "/api/dict";	//判断是否有角色
	//退出接口链接
	var EXIT_API = "/api/logout";

	//响应结果处理
	$.ajaxSetup({
		
		complete:function(xhr, status, data) {
			if(xhr.status == 200 || xhr.status == 0) {
				var json = xhr.responseJSON;
				if(json && json.success == false && json.message) {
					if(typeof(json.message) == "string") {
						$.alert(json.message);
					} else if(typeof(json.message) != "number") {
						$.alert("想绕过校验是木有机会滴.");
					}
				}
				return;
			}
			if(xhr.status == 401) {
				//未认证
				var box = $.alert($.i18n.prop("info.unauthor"), function() {
					//跳转登录页
					window.location.href = LOGIN_LOCATION;
					box.close();
				});
			} else if(xhr.status == 403) {
				$.alert($.i18n.prop("info.forbidden"));
			} else if(xhr.status == 404) {
				$.alert($.i18n.prop("info.no.page"));
			} else {
				var error = $.i18n.prop("info.net.errors");
				error = error.replace("{0}", xhr.status);
				error = error.replace("{1}", xhr.statusText);
				$.alert(error);
			}
		},
		error:function(xhr, options, exc) {
			if(xhr.status == 0) {
				//没有网络错误
				$.alert($.i18n.prop("info.net.error"));
			}
		}
		
	});

	var _ajax = $.ajax;
	//通用方法配置
	$.extend({

		toUrl:function(url) {
			if(url && url.substring(0, 1) == '/') {
				return server + url;
			}
			return url;
		},
		toTplUrl: function(url) {
			var result = url;
			if(result) {
				if(result.substring(0, 1) == '/') {
					result = tpl + result;
				}
				if(result.indexOf(".html") < 0) {
					result += ".html";
				}
			}
			return result;
		},
        ajax : function(obj) {
            obj.url = $.toUrl(obj.url);
            return _ajax(obj);
        },
        perms: function(perms, callback) {
        	$.post(HAS_PERMS, perms, function(data) {
        		if(data.success) {
        			callback(true, data.message);
        		} else {
        			callback(false);
        		}
        	}, "json");
        },
        roles: function(roles, callback) {
        	$.post(HAS_ROLES, roles, function(data) {
        		if(data.success) {
        			callback(true, data.message);
        		} else {
        			callback(false);
        		}
        	}, "json");
        },
        dict:function(opts, callback) {
        	var params = {
        		values:[],
        		children:false
        	}
        	$.extend(params, opts);
        	if(typeof(params.values) == "string") {
        		params.values = [params.values];
        	}
        	$.post(URL_DICT, params, function(data){
        		if(data.success) {
        			callback(true, data.message);
        		} else {
        			callback(false);
        		}
        	}, "json")
        }

	});

	module.exports = {

		getLoginLocation: function() {
			return LOGIN_LOCATION;
		},
		getLogoutLocation: function() {
			return EXIT_API;
		}

	};

});